<?php
class Admin_Form_ValidateProduct {
	
	private $_arrError;
	private $_arrData;
	
	public function __construct($arrParam = null,$options = null){
		//Validate name
		$validate = new Zend_Validate();		
		$validate->addValidator(new Zend_Validate_NotEmpty(),false);
		$options = array('table'=>'product','field'=>'name');
		if($arrParam['action'] == 'edit'){
			$options = array('table'=>'product','field'=>'name',
							 'exclude'=>array('field'=>'id','value'=>$arrParam['id']));
		}
		$validate->addValidator(new Zend_Validate_Db_NoRecordExists($options),false);
		
		if(!$validate->isValid($arrParam['name'])){
			$message = $validate->getMessages();
			$this->_arrError['name'] = current($message);
			$arrParam['name'] = '';
		}
		
		//Validate Pictures
		$upload = new Zend_File_Transfer_Adapter_Http();		
		$fileInfo = $upload->getFileInfo('picture');
		$fileName = $fileInfo['picture']['name'];
		if(!empty($fileName)){
			$options = array('gif','png','jpg');
			$upload->addValidator('Extension',true,$options,'picture')
			 	   ->addValidator('Size',true,array('min'=>'5kb','max'=>'1000kb'),'picture');
				
			if(!$upload->isValid('picture')){
				$message = $upload->getMessages();
				$this->_arrError['picture'] = current($message);
				$arrParam['picture'] = '';
			}
		}else{
			if($arrParam['action'] == 'edit'){
				$arrParam['picture'] = $arrParam['current_picture'];
			}else{
				$this->_arrError['picture'] = 'Please choose a picture';
			}
		}
		
		//Validate Price
		$validate = new Zend_Validate();
		$validate->addValidator(new Zend_Validate_NotEmpty(),false)
				->addValidator(new Zend_Validate_Digits(),false);
		if(!$validate->isValid($arrParam['price'])){
			$message = $validate->getMessages();
			$this->_arrError['price'] = current($message);
			$$arrParam['price'] = '';
		}
		
		//Validate Selloff
		$validate = new Zend_Validate_Digits();
		if($arrParam['selloff'] !=null){
			if(!$validate->isValid($arrParam['selloff'])){
				$message = $validate->getMessages();
				$this->_arrError['selloff'] = current($message);
				$$arrParam['selloff'] = '';
			}
		}
		
		$arrParam['detail'] = $this->filter($arrParam['detail']);
		$arrParam['intro'] = $this->filter($arrParam['intro']);
		$arrParam['promotion'] = $this->filter($arrParam['promotion']);
		$this->_arrData = $arrParam;
	}
	public function isValid(){
		$flag = false;
		if(count($this->_arrError) > 0){
			$flag = true;
		}
		return $flag;
	}
	public function getErrors(){
		return $this->_arrError;
	}
	
	public function getData(){
		if($this->isValid() == false){
			$arrFile = $this->upload();
			if(count($arrFile) > 0){
				$this->_arrData = array_merge($arrFile,$this->_arrData);
			}
		}
		return $this->_arrData;
	}
	public function filter($value){
      	$value = str_replace("\'", "'", $value);
	  	$value = str_replace("\\\'", "\'", $value);
		$value = str_replace('\"', '"', $value);
		$value = str_replace('\\\\', '\\', $value);
    	return $value;
   }
	public function upload(){
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('picture');
		$fileName = $fileInfo['picture']['name'];
		$arrFile = array();
		if(!empty($fileName)){
			$options = array('rename'=>true,'prefix'=>'product_');
			$newName = $upload->upload(PUBLIC_PATH . '/img/product','picture',$options);
			
			$resize = Zendvn_File_Image::create(PUBLIC_PATH . '/img/product/' . $newName);
			$resize->resize(488)->save(PUBLIC_PATH . '/img/product/' . $newName);
	
			$arrFile['picture'] = $newName;
			if($this->_arrData['action'] == 'edit'){
				$upload->remove(PUBLIC_PATH . '/img/product/',$this->_arrData['current_picture']);
				//@unlink(PUBLIC_PATH . '/img/product/' . $this->_arrData['current_picture']);
				echo '</br>' . $this->_arrData['current_picture'];
			}
		}
		return $arrFile;
	}
}